var fullUrl;
import Vue from 'vue'
import App from './App'
import router from './router'
import components from './components/'; // 加载公共组件
import * as filters from './utils/filters'; // 全局vue filter
import utils from './utils'; // 全局vue utils
import ep from './utils/ep'; // 全局vue ep
import store from './vuex'
import MintUi from 'mint-ui'
import Epsoft from 'epsoft-ui';
import 'epsoft-ui/lib/theme/index.css';
import {Indicator, MessageBox} from 'mint-ui';
import './assets/css/basic.css'
import './utils/userAgent'
import 'mint-ui/lib/style.css'
import './components/index'
import axios from 'axios';
import md5 from 'js-md5';
if (process.env.appType) {
  require('./utils/vconsole')
}

Vue.config.productionTip = false
// 全局注册
Vue.prototype.utils = utils;
Vue.prototype.ep = ep;
Vue.prototype.vue = Vue;
Vue.prototype.$http = axios;
Vue.prototype.$md5 = md5;
Vue.prototype.$message = MessageBox;
Vue.use(MintUi);
Vue.use(Epsoft);

axios.defaults.baseURL = process.env.ApiUrl;
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.maxContentLength = 20000,
  Vue.config.productionTip = false
axios.interceptors.request.use(
  config => {
    Indicator.open()
    if (config.url.indexOf('access_token') == -1 && utils.getToken()) {
      if (config.url.indexOf('?') == -1) {
        config.url = config.url + '?access_token=' + utils.getToken();
      } else {
        config.url = config.url + '&access_token=' + utils.getToken();
      }
    
      if (utils.getToken()) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
        config.headers.Authorization = `token ${utils.getToken()}`;
      }
    }
    return config;
  }, error => {
    Indicator.close()
    return Promise.reject(error);
  });
// 添加响应拦截器
axios.interceptors.response.use(
  response => {
    Indicator.close()
    return response.data;
  }, error => {
    Indicator.close();
    let ua = navigator.userAgent.toLowerCase();
    let isWeixin = ua.indexOf('micromessenger') != -1;
    if (isWeixin) {
      if (error.response) {
        if (error.response.status == 404) {

        } else {
          sessionStorage.removeItem("token")
          location.href = fullUrl;
        }
      } else {
        sessionStorage.removeItem("token")
        location.href = fullUrl;
      }
      sessionStorage.removeItem("token");
      location.href = fullUrl;
    }
    return Promise.reject(error);
  });

// 解决嵌入原生APP标题问题
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面meta */
  if (to.meta.content) {
    let head = document.getElementsByTagName('head');
    let meta = document.createElement('meta');
    meta.content = to.meta.content;
    head[0].appendChild(meta)
  }
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }
debugger
//sessionStorage.setItem("token", "ffcd41f5-8abf-481e-8978-b6bd4ef17cea");

if (to.meta.isLogin == "no") {  // 判断该路由是否需要登录权限
  next();
}else {
    if (sessionStorage.getItem("token")) {
        next();
    }
    else{
        next({
            path: '/login',
            query: {redirect: to.fullPath}
        })
    }
}



  // if (to.meta.isLogin == "no") {
  //   next();
  // } else {
    
    // let token = utils.GetUrlParam("token");
    // if (!token) {
    //   token = sessionStorage.getItem("token");
    // }
    // sessionStorage.setItem("token", token);
    
    // axios.post("/user/get/userinfo", {
    //   access_token: sessionStorage.getItem("token")
    // }).then(rest => {
    //   if (rest.success) {
    //     sessionStorage.setItem("userInfo", JSON.stringify(rest.body));
    //     next();
    //   }
    // })
  // }
});
router.afterEach((to, from, next) => {
  MessageBox.close();
})

//注册过滤器
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
});

// 公共组件首字母大写
Object.keys(components).forEach((key) => {
  var name = key.replace(/(\w)/, (v) => v.toUpperCase())
  Vue.component(`ep${name}`, components[key])
});


window.vueInstance = new Vue({
  el: '#app',
  router,
  store,
  components: {
    App
  },
  template: '<App/>'
})
